package cn.edu.scut.sys.controller;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.ShootScoreService;
import cn.edu.scut.sys.service.UnitService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.ShootScore;
import cn.edu.scut.sys.vo.ShootScoreStatistic;
import cn.edu.scut.sys.vo.Student;

@Controller
@RequestMapping("/shootScore")
public class ShootScoreController {

	private ShootScoreService shootScoreService;

	@Resource(name="shootScoreService")
	public void setShootScoreService(ShootScoreService shootScoreService) {
		this.shootScoreService = shootScoreService;
	}
	
	private UnitService unitService;
	
	@Resource(name="unitService")
	public void setUnitService(UnitService unitService) {
		this.unitService = unitService;
	}
	
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<ShootScore> list(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters,
            HttpServletRequest request
            ){  
		 try {  
			 	request.setCharacterEncoding("UTF-8");
	            DataRequest dataRequest = new DataRequest();  
	            dataRequest.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            dataRequest.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            dataRequest.setSidx(sidx);  
	            dataRequest.setSord(sord);  
	            dataRequest.setSearch(search);  
	            dataRequest.setSearchField(searchField);  
	            dataRequest.setSearchOper(searchOper);  
	            dataRequest.setSearchString(searchString);  
	            return shootScoreService.list(dataRequest, request);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/update")
	public String update(String id, String studentId, String trainDate, String totalTime, String stopTime1, 
			String leapDistance1, String stopTime2, String leapDistance2, String stopTime3, 
			String leapDistance3, String fixHit, String moveHit, String gunHit, String score,
			HttpServletRequest request
			){
		
		try{
			// 学员编号
			Student student = new Student();
			student.setId(Long.parseLong(studentId));
			
			// 射击训练成绩
			ShootScore shootScore = shootScoreService.get(Long.parseLong(id));
			shootScore.setStudent(student);
			shootScore.setTrainDate(Timestamp.valueOf(trainDate + " 00:00:00"));
			shootScore.setTotalTime(Integer.parseInt(totalTime));
			shootScore.setStopTime1(Integer.parseInt(stopTime1));
			shootScore.setLeapDistance1(Double.parseDouble(leapDistance1));
			shootScore.setStopTime2(Integer.parseInt(stopTime2));
			shootScore.setLeapDistance2(Double.parseDouble(leapDistance2));
			shootScore.setStopTime3(Integer.parseInt(stopTime3));
			shootScore.setLeapDistance3(Double.parseDouble(leapDistance3));
			shootScore.setFixHit(Integer.parseInt(fixHit));
			shootScore.setMoveHit(Integer.parseInt(moveHit));
			shootScore.setGunHit(Integer.parseInt(gunHit));
			shootScore.setScore(Integer.parseInt(score));
			
			shootScoreService.update(shootScore);
			
			request.setAttribute("status", 1);
			return "/page/shootScoreEdit";
		}catch(Exception ex){
			request.setAttribute("status", 0);
			return "/page/shootScoreEdit";
		}
		
	}
	
	
	@RequestMapping("/adds")
	public String adds(HttpServletRequest request){
		
		try{
			
			String[] studentIds = request.getParameterValues("studentId");
			String[] trainDates = request.getParameterValues("trainDate");
			String[] totalTimes = request.getParameterValues("totalTime");
			String[] stopTime1s = request.getParameterValues("stopTime1");
			String[] stopTime2s = request.getParameterValues("stopTime2");
			String[] stopTime3s = request.getParameterValues("stopTime3");
			String[] leapDistance1s = request.getParameterValues("leapDistance1");
			String[] leapDistance2s = request.getParameterValues("leapDistance2");
			String[] leapDistance3s = request.getParameterValues("leapDistance3");
			String[] fixHits = request.getParameterValues("fixHit");
			String[] moveHits = request.getParameterValues("moveHit");
			String[] gunHits = request.getParameterValues("gunHit");
			String[] scores = request.getParameterValues("score");
			
			for(int i = 0; i < studentIds.length; i++){
				// 学员
				Student student = new Student();
				student.setId(Long.parseLong(studentIds[i]));
				
				// 射击训练成绩
				ShootScore shootScore = new ShootScore();
				shootScore.setStudent(student);
				shootScore.setTrainDate(Timestamp.valueOf(trainDates[i] + " 00:00:00"));
				shootScore.setTotalTime(Integer.parseInt(totalTimes[i]));
				shootScore.setStopTime1(Integer.parseInt(stopTime1s[i]));
				shootScore.setLeapDistance1(Double.parseDouble(leapDistance1s[i]));
				shootScore.setStopTime2(Integer.parseInt(stopTime2s[i]));
				shootScore.setLeapDistance2(Double.parseDouble(leapDistance2s[i]));
				shootScore.setStopTime3(Integer.parseInt(stopTime3s[i]));
				shootScore.setLeapDistance3(Double.parseDouble(leapDistance3s[i]));
				shootScore.setFixHit(Integer.parseInt(fixHits[i]));
				shootScore.setMoveHit(Integer.parseInt(moveHits[i]));
				shootScore.setGunHit(Integer.parseInt(gunHits[i]));
				shootScore.setScore(Integer.parseInt(scores[i])); 
				shootScoreService.add(shootScore);
			}
			request.setAttribute("status", 1);
			return "/page/shootScoreAdds";

		}catch(Exception ex){
			request.setAttribute("status", 0);
			return "/page/shootScoreAdds";
		}

	}
	
	@RequestMapping("/toAdds")
	public String toAdds(HttpServletRequest request){
		request.setAttribute("unitByNowGroupList", unitService.listByUnitId(1L));
		return "/page/shootScoreAdds";
	}
	@RequestMapping("/toEdit/{id}")
	public String toEdit(@PathVariable(value = "id") Long id, HttpServletRequest request){
		request.setAttribute("shootScore", shootScoreService.get(id));
		return "/page/shootScoreEdit";
	}
	
	@RequestMapping("/del/{id}")
	public String del(@PathVariable(value = "id") int id,HttpServletRequest request){
		try{
			shootScoreService.del(id);
			request.setAttribute("dstatus", 1);
		}
		catch(Exception ex){
			ex.printStackTrace();
			request.setAttribute("dstatus", 0);
		}
	return "/page/shootScoreList";
	}
	
	@RequestMapping("/statistic")
	@ResponseBody
	public DataResponse<ShootScoreStatistic> statistic(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters,
            HttpServletRequest request
            ){  
		 try {  
			 	request.setCharacterEncoding("UTF-8");
	            DataRequest dataRequest = new DataRequest();  
	            dataRequest.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            dataRequest.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            dataRequest.setSidx(sidx);  
	            dataRequest.setSord(sord);  
	            dataRequest.setSearch(search);  
	            dataRequest.setSearchField(searchField);  
	            dataRequest.setSearchOper(searchOper);  
	            dataRequest.setSearchString(searchString);  
	            return shootScoreService.statistic(dataRequest, request);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/analyze")
	@ResponseBody
	public DataResponse<ShootScore> analyze(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters,
            HttpServletRequest request
            ){  
		 try {  
			 	request.setCharacterEncoding("UTF-8");
	            DataRequest dataRequest = new DataRequest();  
	            dataRequest.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            dataRequest.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            dataRequest.setSidx(sidx);  
	            dataRequest.setSord(sord);  
	            dataRequest.setSearch(search);  
	            dataRequest.setSearchField(searchField);  
	            dataRequest.setSearchOper(searchOper);  
	            dataRequest.setSearchString(searchString);  
	            return shootScoreService.analyze(dataRequest, request);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
}
